package models.base;

import facade.vo.base.RoleBusinessVO;
import helper.GlobalConfig;
import models.common.BaseModel;
import models.constants.DeletedStatus;
import org.apache.commons.lang.StringUtils;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import play.db.jpa.JPA;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Query;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * created by weiwei on 2018/02/08
 * 业务表，
 */


@Entity
@Table(name = "admin_business", schema = GlobalConfig.MIGAO_DB_SCHEMA)
public class AdminBusiness extends BaseModel {


    /**
     * 业务编号
     */
    @Column(name = "code")
    public String code;

    /**
     * 业务名称
     */
    @Column(name = "name")
    public String name;


    /**
     * Controller名称
     */
    @Column(name = "controller")
    public String controller;

    /**
     * 页面请求地址
     */
    @Column(name = "path")
    public String path;

    /**
     * 排序号
     */
    @Column(name = "show_order")
    public Integer showOrder = 0;

    @Column(name = "remark")
    public String remark;



    public static AdminBusiness findOne(String id){
        return AdminBusiness.find("deleted = ? and id = ? " , DeletedStatus.UN_DELETED , id).first();
    }


    public static AdminBusiness findLastOne(){
        return AdminBusiness.find("deleted = ? order by createdAt desc" , DeletedStatus.UN_DELETED).first();
    }

    //获取所有业务
    public static List<AdminBusiness> findAllBusiness(){
        return AdminBusiness.find("deleted = ? order by showOrder,code asc" , DeletedStatus.UN_DELETED).fetch();
    }

    //获取所有业务
    public static List<RoleBusinessVO> findByRoleId(String roleId){
        if (StringUtils.isEmpty(roleId)) return Collections.emptyList();
        List<RoleBusinessVO> resultList = new ArrayList<>();
        String sqlSelect = "SELECT b.id as businessId,b.name,b.code,ro.is_search as isSearch,ro.is_add as isAdd,ro.is_edit as isEdit,ro.is_delete as isDelete from admin_business b left join admin_role_operates ro on ro.business_id = b.id and ro.deleted=0 and ro.role_id='"+roleId+"' where b.deleted=0";
        System.out.println(sqlSelect);
        Query query = JPA.em().createNativeQuery(sqlSelect);
        query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List<Map> voList = query.getResultList();
        if(voList != null && voList.size()>0){
            for (Map map : voList) {
                if(map.get("code") == null) continue;

                RoleBusinessVO vo = new RoleBusinessVO();
                vo.businessId = map.get("businessId").toString();
                vo.code = map.get("code").toString();
                vo.name = map.get("name").toString();
                vo.isSearch = Boolean.valueOf(String.valueOf(map.get("isSearch")));
                vo.isAdd = Boolean.valueOf(String.valueOf(map.get("isAdd")));
                vo.isEdit = Boolean.valueOf(String.valueOf(map.get("isEdit")));
                vo.isDelete = Boolean.valueOf(String.valueOf(map.get("isDelete")));
                resultList.add(vo);
            }
        }
        return resultList;
    }

    public static Boolean existByName(String name){
        if(StringUtils.isEmpty(name)) return null;
        return count("deleted=? and name = ?",DeletedStatus.UN_DELETED,name)>0;
    }

}
